package com.sqllog.admin.framework.config;

import com.mysql.jdbc.AbandonedConnectionCleanupThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.MongoDatabaseUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;

/**
 * 
 * @ClassName: ContextFinalizer
 * @Description: 关闭tomcat时,同时关闭数据库链接超时检测线程
 * @author eno
 * @date :2019年12月20日 下午12:20:36
 */
@WebListener
public class ContextFinalizer implements ServletContextListener {
	private static final Logger logger = LoggerFactory.getLogger(ContextFinalizer.class);

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		ServletContextListener.super.contextInitialized(sce);
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		Enumeration<Driver> drivers = DriverManager.getDrivers();
		while (drivers.hasMoreElements()) {
			Driver driver = drivers.nextElement();
			try {
				DriverManager.deregisterDriver(driver);
			} catch (SQLException e) {
				logger.error("ContextFinalizer: deregisterDriver failed,{}", e);
			}
		}

		AbandonedConnectionCleanupThread.checkedShutdown();
	}
}
